<?php

namespace common\models\db\tables;

use Yii;

/**
 * This is the model class for table "{{%menu}}".
 *
 * @property string $id
 * @property string $root 父级菜单
 * @property int $lft Nested set left property
 * @property int $rgt Nested set right property
 * @property int $lvl Nested set level / depth
 * @property string $name The tree node name / label
 * @property string $icon The icon to use for the node
 * @property int $icon_type Icon Type: 1 = CSS Class, 2 = Raw Markup
 * @property int $active Whether the node is active (will be set to false on deletion)
 * @property int $selected Whether the node is selected/checked by default
 * @property int $disabled Whether the node is enabled
 * @property int $readonly Whether the node is read only (unlike disabled - will allow toolbar actions)
 * @property int $visible Whether the node is visible
 * @property int $collapsed Whether the node is collapsed by default
 * @property int $movable_u Whether the node is movable one position up
 * @property int $movable_d Whether the node is movable one position down
 * @property int $movable_l Whether the node is movable to the left (from sibling to parent)
 * @property int $movable_r Whether the node is movable to the right (from sibling to child)
 * @property int $removable Whether the node is removable (any children below will be moved as siblings before deletion)
 * @property int $removable_all Whether the node is removable along with descendants
 * @property int $child_allowed Whether to allow adding children to the node
 * @property string $yii2_app_id 应用id
 * @property string $uri 路由
 * @property int $created_at 创建时间
 * @property string $created_by 创建者
 * @property int $updated_at 修改时间
 * @property string $updated_by 修改者
 * @property string $desc 介绍
 * @property int $sort 排序(默认从小到大)
 * @property int $status 状态
 *
 * @property Menu $root0
 * @property Menu[] $menus
 * @property Yii2App $yii2App
 * @property User $createdBy
 * @property User $updatedBy
 * @property RoleMenu[] $roleMenus
 * @property Role[] $roles
 */
class Menu extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%menu}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['root', 'lft', 'rgt', 'lvl', 'icon_type', 'active', 'selected', 'disabled', 'readonly', 'visible', 'collapsed', 'movable_u', 'movable_d', 'movable_l', 'movable_r', 'removable', 'removable_all', 'child_allowed', 'yii2_app_id', 'created_at', 'created_by', 'updated_at', 'updated_by', 'sort', 'status'], 'integer'],
            [['lft', 'rgt', 'lvl', 'name', 'yii2_app_id', 'uri', 'created_at'], 'required'],
            [['desc'], 'string'],
            [['name', 'icon', 'uri'], 'string', 'max' => 191],
            [['yii2_app_id', 'uri'], 'unique', 'targetAttribute' => ['yii2_app_id', 'uri']],
            [['root'], 'exist', 'skipOnError' => true, 'targetClass' => Menu::className(), 'targetAttribute' => ['root' => 'id']],
            [['yii2_app_id'], 'exist', 'skipOnError' => true, 'targetClass' => Yii2App::className(), 'targetAttribute' => ['yii2_app_id' => 'id']],
            [['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['created_by' => 'id']],
            [['updated_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['updated_by' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'root' => Yii::t('app', '父级菜单'),
            'lft' => Yii::t('app', 'Nested set left property'),
            'rgt' => Yii::t('app', 'Nested set right property'),
            'lvl' => Yii::t('app', 'Nested set level / depth'),
            'name' => Yii::t('app', 'The tree node name / label'),
            'icon' => Yii::t('app', 'The icon to use for the node'),
            'icon_type' => Yii::t('app', 'Icon Type: 1 = CSS Class, 2 = Raw Markup'),
            'active' => Yii::t('app', 'Whether the node is active (will be set to false on deletion)'),
            'selected' => Yii::t('app', 'Whether the node is selected/checked by default'),
            'disabled' => Yii::t('app', 'Whether the node is enabled'),
            'readonly' => Yii::t('app', 'Whether the node is read only (unlike disabled - will allow toolbar actions)'),
            'visible' => Yii::t('app', 'Whether the node is visible'),
            'collapsed' => Yii::t('app', 'Whether the node is collapsed by default'),
            'movable_u' => Yii::t('app', 'Whether the node is movable one position up'),
            'movable_d' => Yii::t('app', 'Whether the node is movable one position down'),
            'movable_l' => Yii::t('app', 'Whether the node is movable to the left (from sibling to parent)'),
            'movable_r' => Yii::t('app', 'Whether the node is movable to the right (from sibling to child)'),
            'removable' => Yii::t('app', 'Whether the node is removable (any children below will be moved as siblings before deletion)'),
            'removable_all' => Yii::t('app', 'Whether the node is removable along with descendants'),
            'child_allowed' => Yii::t('app', 'Whether to allow adding children to the node'),
            'yii2_app_id' => Yii::t('app', '应用id'),
            'uri' => Yii::t('app', '路由'),
            'created_at' => Yii::t('app', '创建时间'),
            'created_by' => Yii::t('app', '创建者'),
            'updated_at' => Yii::t('app', '修改时间'),
            'updated_by' => Yii::t('app', '修改者'),
            'desc' => Yii::t('app', '介绍'),
            'sort' => Yii::t('app', '排序(默认从小到大)'),
            'status' => Yii::t('app', '状态'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRoot0()
    {
        return $this->hasOne(Menu::className(), ['id' => 'root']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMenus()
    {
        return $this->hasMany(Menu::className(), ['root' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getYii2App()
    {
        return $this->hasOne(Yii2App::className(), ['id' => 'yii2_app_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCreatedBy()
    {
        return $this->hasOne(User::className(), ['id' => 'created_by']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUpdatedBy()
    {
        return $this->hasOne(User::className(), ['id' => 'updated_by']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRoleMenus()
    {
        return $this->hasMany(RoleMenu::className(), ['menu_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRoles()
    {
        return $this->hasMany(Role::className(), ['id' => 'role_id'])->viaTable('{{%role_menu}}', ['menu_id' => 'id']);
    }
}
